package com.contest.service.impl;

import com.contest.dao.BettingRecordDao;
import com.contest.dao.RoomInfoDao;
import com.contest.entity.BettingRecord;
import com.contest.service.BettingRecordService;
import com.contest.service.RoomInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * ${table.comment}  服务实现类
 * </p>
 *
 * @author yfl
 * @since ${date}
 */
@Service
public class BettingRecordServiceImpl extends ServiceImpl<BettingRecordDao, BettingRecord> implements BettingRecordService {

    @Autowired
    BettingRecordDao bettingRecordDao;

    @Autowired
    private RoomInfoDao roomInfoDao;

    @Override
    public List<BettingRecord> todayTop() {
        List<BettingRecord> todayTop = bettingRecordDao.todayTop();
        return todayTop;
    }

    /**
     * 单日战队下注排行
     *
     * @return
     */
    @Override
    public List<BettingRecord> todayClanTop() {
        List<BettingRecord> todayClanTop = bettingRecordDao.todayClanTop();
        return todayClanTop;
    }

    /**
     * 战队胜利总场数
     *
     * @return
     */
    @Override
    public List<Map<String,Object>> clanTotalWin() {
        List<Map<String, Object>> aMapList = bettingRecordDao.countA();
        List<Map<String, Object>> bMapList = bettingRecordDao.countB();
        if (aMapList.size() > bMapList.size()) {
            return mergeCollection(aMapList, bMapList);
        } else {
            return mergeCollection(bMapList, aMapList);
        }

    }

    /**
     * 用户当日下注总额
     *
     * @param userId
     */
    @Override
    public Float getTodayByUserId(String userId) {
        return bettingRecordDao.getTodayByUserId(userId);
    }

    /**
     * 用户当月下注总额
     *
     * @param userId
     */
    @Override
    public Float getMonByUserId(String userId) {
        return bettingRecordDao.getMonByUserId(userId);
    }

    /**
     * 用户当日赢总额
     *
     * @param userId
     */
    @Override
    public Float getTodayWinByUserId(String userId) {
        return bettingRecordDao.getTodayWinByUserId(userId);
    }

    /**
     * 用户当日输总额
     *
     * @param userId
     */
    @Override
    public Float getTodayLoseByUserId(String userId) {
        return bettingRecordDao.getTodayLoseByUserId(userId);
    }

    /**
     * 用户当月赢总额
     *
     * @param userId
     */
    @Override
    public Float getMonWinByUserId(String userId) {
        return bettingRecordDao.getMonWinByUserId(userId);
    }

    /**
     * 用户当月输总额
     *
     * @param userId
     */
    @Override
    public Float getMonLoseByUserId(String userId) {
        return bettingRecordDao.getMonLoseByUserId(userId);
    }

    private List<Map<String,Object>> mergeCollection(List<Map<String, Object>> aMapList, List<Map<String, Object>> bMapList) {
        for (int j = 0; j < aMapList.size(); j++) {
            Map<String, Object> aMap = aMapList.get(j);
            String aId = aMap.get("clanId").toString();
            for (int i = 0; i < bMapList.size(); i++) {
                String bId = bMapList.get(i).get("clanId").toString();
                if (aId.equals(bId)) {
                    int tempTotal = Integer.parseInt(aMap.get("total").toString()) + Integer.parseInt(bMapList.get(i).get("total").toString());
                    aMap.put("total", tempTotal + "");
                }
            }
        }
        aMapList.addAll(bMapList);
        //Object[] tempArr = aMapList.toArray();
        for (int i = 0; i < aMapList.size(); i++) {
            for (int j = 0; j < aMapList.size() - 1 - i; j++) {
                Map<String, Object> tempA = aMapList.get(j);
                Map<String, Object> tempB = aMapList.get(j + 1);
                Map<String,Object> temp = null;
                if (Integer.parseInt(tempA.get("total").toString()) < Integer.parseInt(tempB.get("total").toString())) {
                    temp = aMapList.get(j);
                    aMapList.set(j, aMapList.get(j + 1));
                    aMapList.set(j + 1, temp);
                }
            }
        }
        for(Map<String,Object> m : aMapList){
            String id = String.valueOf(m.get("clanId"));
            Map<String,Object> ma = new HashMap<>();
            ma.put("fd_clan_a_id",id);
            ma.put("fd_is_settle","1");
            List anum = roomInfoDao.selectByMap(ma);

            Map<String,Object> ma2 = new HashMap<>();
            ma2.put("fd_clan_b_id",id);
            ma2.put("fd_is_settle","1");
            List bnum = roomInfoDao.selectByMap(ma2);

            if(anum != null && bnum != null){
                DecimalFormat df = new DecimalFormat("######0.00");
                double num = anum.size() + bnum.size();
                double t = Double.parseDouble(String.valueOf(m.get("total")));
                if(num != 0.0) {
                    String winrate = df.format((t / num) * 100) + "%";
                    m.put("winrate",winrate);
                }else{
                    m.put("winrate","无比赛记录");
                }
            }
        }
        return aMapList;
    }
}
